package com.exartisan.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.exartisan.bean.Event;
import com.exartisan.util.*;
import com.exartisan.yn_event.R;
import com.loopj.android.image.SmartImageView;

public class HomeGridAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private Context ctx;
	private ArrayList<Event> events;

	public HomeGridAdapter(Context context, ArrayList<Event> events) {
		inflater = LayoutInflater.from(context);
		this.ctx = context;
		this.events = events;
	}

	@Override
	public int getCount() {
		return events.size();
	}

	@Override
	public Object getItem(int pos) {
		return events.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return events.get(pos).getEventID();
	}

	@Override
	public View getView(int pos, View view, ViewGroup parrent) {
		ViewHolder holder;
		if (view == null){
			holder = new ViewHolder();
		view = inflater.inflate(R.layout.event_category, null);
		holder.imgCover = (SmartImageView) view.findViewById(R.id.img_event);
		holder.tvEventName = (TextView) view.findViewById(R.id.tv_event_name);
		holder.tvEventTime = (TextView) view.findViewById(R.id.tv_event_time);
		holder.tvEventName.setTypeface(FontUtil.loadFontFromAssets(ctx, FontUtil.TAHOMABD));
		holder.tvEventTime.setTypeface(FontUtil.loadFontFromAssets(ctx, FontUtil.ARIAL));
		view.setTag(holder);
		} else
			holder = (ViewHolder) view.getTag();
		holder.tvEventName.setText(events.get(pos).getEventName());
		holder.tvEventTime.setText(DateTimeFormat.format(events.get(pos).getStartDate(), DateTimeFormat.SHORT_VALUE));
		holder.imgCover.setImageUrl(events.get(pos).getThumbUrl());
		return view;
	}
	
	class ViewHolder {
		SmartImageView imgCover;
		TextView tvEventName;
		TextView tvEventTime;
	}

}
